package com.ruoyi.web.controller.sxt;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SxtBumen;
import com.ruoyi.common.core.domain.entity.SxtJuese;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.sxt.domain.SxtGangwei;
import com.ruoyi.sxt.domain.SxtYonghu;
import com.ruoyi.sxt.service.ISxtBumenService;
import com.ruoyi.sxt.service.ISxtGangweiService;
import com.ruoyi.sxt.service.ISxtJueseService;
import com.ruoyi.sxt.service.ISxtYonghuService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2025-06-22
 */
@Controller
@RequestMapping("/sxt/yonghu")
public class SxtYonghuController extends BaseController
{
    private String prefix = "sxt/yonghu";

    @Autowired
    private ISxtYonghuService sxtYonghuService;

    @Autowired
    private ISxtBumenService sxtBumenService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISxtJueseService sxtJueseService;

    @Autowired
    private ISxtGangweiService sxtGangweiService;


    @RequiresPermissions("sxt:yonghu:view")
    @GetMapping()
    public String yonghu()
    {
        return prefix + "/yonghu";
    }

    /**
     * 查询用户信息列表
     */
    @RequiresPermissions("sxt:yonghu:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SxtYonghu sxtYonghu)
    {
        startPage();
        List<SxtYonghu> list = sxtYonghuService.selectSxtYonghuList(sxtYonghu);
        if(list != null && list.size() > 0){
            for(SxtYonghu sxtYonghu1 : list){
                SxtBumen sxtBumen = sxtBumenService.selectSxtBumenByDeptId(sxtYonghu1.getDeptId());
                sxtYonghu1.setBumenName(sxtBumen.getDeptName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @RequiresPermissions("sxt:yonghu:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SxtYonghu sxtYonghu)
    {
        List<SxtYonghu> list = sxtYonghuService.selectSxtYonghuList(sxtYonghu);
        ExcelUtil<SxtYonghu> util = new ExcelUtil<SxtYonghu>(SxtYonghu.class);
        return util.exportExcel(list, "用户信息数据");
    }

    /**
     * 新增用户信息
     */
    @RequiresPermissions("sxt:yonghu:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<SxtJuese> jueseList = sxtJueseService.selectSxtJueseList(new SxtJuese());
        if(jueseList != null && jueseList.size() > 0){
            for(SxtJuese juese : jueseList){
                if(juese.getRoleId() == 1){
                    jueseList.remove(juese);
                    break;
                }
            }
        }
        mmap.put("roles", jueseList);
        mmap.put("posts", sxtGangweiService.selectSxtGangweiList(new SxtGangwei()));
        return prefix + "/add";
    }

    /**
     * 新增保存用户信息
     */
    @RequiresPermissions("sxt:yonghu:add")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SxtYonghu sxtYonghu)
    {
        return toAjax(sxtYonghuService.insertSxtYonghu(sxtYonghu));
    }

    /**
     * 修改用户信息
     */
    @RequiresPermissions("sxt:yonghu:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        // 获取用户所属的角色列表
        List<SxtJuese> jueseList = sxtYonghuService.selectSxtJueseByYonghuId(userId);
        //获取用户所属的岗位列表
        List<SxtGangwei> gangweiList = sxtYonghuService.selectSxtGangweiByYonghuId(userId);
        SxtYonghu sxtYonghu = sxtYonghuService.selectSxtYonghuByUserId(userId);
        sxtYonghu.setBumenName(sxtBumenService.selectSxtBumenByDeptId(sxtYonghu.getDeptId()).getDeptName());
        mmap.put("sxtYonghu", sxtYonghu);
        mmap.put("jueseList", jueseList);
        mmap.put("gangweiList",gangweiList);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户信息
     */
    @RequiresPermissions("sxt:yonghu:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SxtYonghu sxtYonghu)
    {
        return toAjax(sxtYonghuService.updateSxtYonghu(sxtYonghu));
    }

    /**
     * 删除用户信息
     */
    @RequiresPermissions("sxt:yonghu:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sxtYonghuService.deleteSxtYonghuByUserIds(ids));
    }

    /**
     * 加载部门列表树
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/deptTreeData")
    @ResponseBody
    public List<Ztree> deptTreeData()
    {
        List<Ztree> ztrees = sxtBumenService.selectDeptTree(new SxtBumen());
        return ztrees;
    }

    /**
     * 进入授权角色页
     */
    @RequiresPermissions("system:user:edit")
    @GetMapping("/authRole/{userId}")
    public String authRole(@PathVariable("userId") Long userId, ModelMap mmap)
    {

        SxtYonghu sxtYonghu = sxtYonghuService.selectSxtYonghuByUserId(userId);
        // 获取用户所属的角色列表
        List<SxtJuese> jueseList = sxtYonghuService.selectSxtJueseByYonghuId(userId);
        mmap.put("user", sxtYonghu);
        mmap.put("roles", jueseList);
        return prefix + "/authRole";
    }

    /**
     * 用户授权角色
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PostMapping("/authRole/insertAuthRole")
    @ResponseBody
    public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
    {
        sxtYonghuService.insertUserAuth(userId, roleIds);
        return success();
    }

    /**
     * 选择部门树
     *
     * @param deptId 部门ID
     */
    @RequiresPermissions("system:user:list")
    @GetMapping("/selectDeptTree/{deptId}")
    public String selectDeptTree(@PathVariable("deptId") Long deptId, ModelMap mmap)
    {
        mmap.put("dept",sxtBumenService.selectSxtBumenByDeptId(deptId));
        return prefix + "/deptTree";
    }
}
